{% template "admin/header.html" %}
<style>
    .layui-anim-upbit{
        z-index: 99999 !important;
    }
</style>
<div class="admin-main">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>添加文章</legend>
    </fieldset>
    <form class="layui-form layui-form-pane" lay-filter="form">
        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-4">
                <input type="text" name="title" value="" lay-verify="required" placeholder="标题" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" >
            <label class="layui-form-label">分类</label>
            <div class="layui-input-4">
                <select name="type_id" lay-verify="required" lay-filter="pid" id="auths">
                    <option value="">请选择分类</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">跳转链接</label>
            <div class="layui-input-4">
                <input type="text" name="fromlink" value="" lay-verify="required" placeholder="URL" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">关键字</label>
            <div class="layui-input-4">
                <textarea class="layui-textarea" name="keywords"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">简介</label>
            <div class="layui-input-4">
                <textarea class="layui-textarea" name="desc"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">图片</label>
            <input type="hidden" name="picture" id="picture">
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class="layui-btn layui-btn-primary" id="adBtn"><i class="icon icon-upload3"></i>点击上传</button>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" src="" id="adPic">
                        <p id="demoText"></p>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">文章内容</label>
            <div class="layui-input-4">
                <textarea id="editor_id" name="content" style="width:700px;height:300px;"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="is_open"  value="1" title="展示" >
                <input type="radio" name="is_open" value="0" title="隐藏">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-4">
                <input type="number" name="sort" value="100" lay-verify="required" placeholder="排序" class="layui-input">
            </div>
        </div>
        <input type="hidden" name="id" >
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="auth">立即提交</button>
                <a href="javascript:window.history.back();" class="layui-btn layui-btn-primary">返回</a>
            </div>
        </div>
    </form>
</div>

{% template "admin/footer.html" %}
<script src="/static/ueditor/ueditor.config.js"></script>
<script src="/static/ueditor/ueditor.all.min.js"></script>

<script>
    let aid = getUrlParam("id");
    layui.use(['form', 'layer','upload'], function () {
        var form = layui.form,layer = layui.layer,$ = layui.jquery,upload = layui.upload;

        var ue = null;
        $.post('/admin/article/get',{id:aid},function (res) {
            let formdata = upperJSONKey(res.Data);
            form.val("form", formdata);

            $("#adPic").attr("src",formdata.picture);

            $.post('/admin/article_type/query',function (data) {
                data = hanMenu(data.Data,0,0);
                $.each(data,function (k,v) {
                    $('#auths').append('<option value="'+v.Id+'" '+(v.Id==formdata.type_id?'selected':'')+'>'+v.ltitle+'</option>');
                });
                form.render('select');
            });

            var ue = UE.getEditor('editor_id');
        });

        form.on('submit(auth)', function (data) {
            // 提交到方法 默认为本身
            $.post("/admin/article/edit",data.field,function(res){
                if(res.Code > 0){
                    layer.msg(res.Msg,{time:1800,icon:1},function(){
                        window.history.back();
                    });
                }else{
                    layer.msg(res.Msg,{time:1800,icon:2});
                }
            });
        });

        function hanMenu(data,pid,level){
            var arr = [];
            var tem = null;
            for (var i=0; i<data.length; i++){
                if(data[i].Pid == pid){
                    tem = data[i];
                    tem['lvl'] = level + 1;
                    tem['lefthtml'] = "|— ".repeat(level);
                    tem['ltitle'] = tem['lefthtml'] + tem.Title;
                    arr.push(tem);
                    arr = arr.concat(hanMenu(data,data[i].Id,tem['lvl']));
                }
            }
            return arr;
        }


        //普通图片上传
        var uploadInst = upload.render({
            elem: '#adBtn'
            ,url: '/upload/file'
            ,data:{path:'article'}
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#adPic').attr('src', result); //图片链接（base64）
                });
            },
            done: function(res){
                if(res.Code>0){
                    $('#picture').val(res.Data);
                }else{
                    //如果上传失败
                    return layer.msg('上传失败');
                }
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
    })
</script>
</body>
</html>
